package com.jd.jr.pay.demo.action;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jd.jr.pay.demo.model.UserInfoVO;
import com.jd.jr.pay.demo.util.MD5Util;
import com.jd.jr.pay.demo.util.PropertyUtils;

/**
 */
@Controller
public class UserInfoAction {

	/**
	 * @param httpServletRequest
	 * @return
	 * 查询用户信息
	 */
	@RequestMapping(value = "/userInfoStart.htm")
	public String userInfoStart(HttpServletRequest httpServletRequest) {
		httpServletRequest.setAttribute("mercId", PropertyUtils.getProperty("9f.mercId"));
		httpServletRequest.setAttribute("mblNo", PropertyUtils.getProperty("9f.user.uMblNo"));
		return "userInfoStart";
	}

	@RequestMapping(value = "/userInfoDo.htm", method = RequestMethod.POST,produces="application/json;charset=UTF-8")
	@ResponseBody
	public String userInfoDo(UserInfoVO userInfoVO, HttpServletRequest httpServletRequest) throws Exception {
		//加入验签
		String userInfoVOSign = getUserInfoVOSign(userInfoVO, PropertyUtils.getProperty("9f.salt"));
		userInfoVO.setHmac(StringUtils.lowerCase(MD5Util.md5(userInfoVOSign)));
		
		String postParam = postParam(PropertyUtils.getProperty("9f.portal.usrCredit"),objectToMap(userInfoVO));

		return postParam;
	}
	
	
	public String getUserInfoVOSign(UserInfoVO userInfoVO,String key){
		return userInfoVO.getCharset()+
				userInfoVO.getMercId()+
				userInfoVO.getInterfaceName()+
				userInfoVO.getVersion()+
				userInfoVO.getSignType()+
				userInfoVO.getMblNo()+
				key;
	}
	public static Map<String, Object> objectToMap(Object obj) throws Exception {    
        if(obj == null)  
            return null;      
  
        Map<String, Object> map = new HashMap<String, Object>();   
  
        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());    
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();    
        for (PropertyDescriptor property : propertyDescriptors) {    
            String key = property.getName();    
            if (key.compareToIgnoreCase("class") == 0) {   
                continue;  
            }  
            Method getter = property.getReadMethod();  
            Object value = getter!=null ? getter.invoke(obj) : null;  
            map.put(key, value);  
        }    
  
        return map;  
    }   
	public String postParam(String url,Map<String, Object> map) throws Exception {
		CloseableHttpClient httpClient = HttpClients.createDefault();

		HttpPost httpPost = new HttpPost(url);
		// 创建一个虚拟的from表单
		List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
		for (Map.Entry<String, Object> entry : map.entrySet()) {
			parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
		}
		// 构造一个form表单式的实体
		UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters, "UTF-8");
		// 把form给请求对象
		httpPost.setEntity(formEntity);

		CloseableHttpResponse response = httpClient.execute(httpPost);
		if (response.getStatusLine().getStatusCode() == 200) {
			HttpEntity entity = response.getEntity();
			String content = EntityUtils.toString(entity);
			System.out.println("----------------------------" + content);
			return content;
		}
		return null;
	}
}
